package com.example.taobaounion.view.activity.impl;

import android.content.ClipData;
import android.content.ClipboardManager;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.os.Build;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.example.taobaounion.R;
import com.example.taobaounion.base.BaseActivity;
import com.example.taobaounion.manager.PresenterManager;
import com.example.taobaounion.model.domain.TicketResult;
import com.example.taobaounion.presenter.impl.TicketPresenter;
import com.example.taobaounion.utils.ToastUtils;
import com.example.taobaounion.utils.UrlUtils;
import com.example.taobaounion.view.callback.ITicketPagerCallBack;
import com.example.taobaounion.view_component.LoadingView;

import butterknife.BindView;

public class TicketActivity extends BaseActivity {

    public static final String TAG = "TicketActivity";
    private TicketPresenter mTicketPresenter;
    private ITicketPagerCallBack mITicketPagerCallBack;


    @BindView(R.id.ticket_cover)
    public ImageView mCover;

    @BindView(R.id.ticket_cover_loading)
    public LoadingView mCoverLoading;

    @BindView(R.id.ticket_cover_error)
    public TextView mCoverError;

    @BindView(R.id.ticket_back)
    public ImageView mBackBtn;

    @BindView(R.id.ticket_code)
    public EditText mTicketCode;

    @BindView(R.id.ticket_copy_or_open_btn)
    public TextView mOpenOrCopyBtn;

    private boolean mHasTaobaoApp;


    protected void initContentView() {
        setContentView(R.layout.activity_ticket);
    }

    protected void initView() {
        mOpenOrCopyBtn.setText(mHasTaobaoApp ? "打开淘宝领劵" : "复制口令");
    }

    protected void initData() {
        // check if have tao bao application
        PackageManager packageManager = this.getPackageManager();

        try {
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
                PackageInfo packageInfo = packageManager.getPackageInfo("com.taobao.taobao", PackageManager.MATCH_UNINSTALLED_PACKAGES);

                mHasTaobaoApp = packageInfo != null;
            }
        } catch (PackageManager.NameNotFoundException e) {
            e.printStackTrace();
        }
    }

    protected void initListener() {
        mBackBtn.setOnClickListener(v -> finish());

        mCoverError.setOnClickListener(v -> {

        });

        mOpenOrCopyBtn.setOnClickListener(v -> {
            // copy code from tao bao
            String ticketCode = mTicketCode.getText().toString().trim();

            // 获取剪贴板管理对象
            ClipboardManager systemService = (ClipboardManager) getSystemService(Context.CLIPBOARD_SERVICE);

            ClipData sob_taobao_code = ClipData.newPlainText("sob_taobao_code", ticketCode);

            // 将淘口令放到剪贴板
            assert systemService != null;
            systemService.setPrimaryClip(sob_taobao_code);


            if (mHasTaobaoApp) {
                // open tao bao application
                Intent taobaoIntent = new Intent();

//                taobaoIntent.setAction("android.intent.action.MAIN");
//                taobaoIntent.addCategory("android.intent.category.LAUNCHER");
                // 跳转到淘宝的欢迎界面
                taobaoIntent.setComponent(new ComponentName("com.taobao.taobao", "com.taobao.tao.welcome.Welcome"));

                startActivity(taobaoIntent);
            } else {
                // copy ok
                ToastUtils.showToast("复制成功");
            }
        });
    }

    void setCurrentStateView(View view) {
        mCover.setVisibility(View.GONE);
        mCoverError.setVisibility(View.GONE);
        mCoverLoading.setVisibility(View.GONE);

        if (view != null) {
            view.setVisibility(View.VISIBLE);
        }
    }

    protected void initPresenter() {
        mTicketPresenter = PresenterManager.getInstance().getTicketPresenter();
        if (mTicketPresenter != null) {

            mITicketPagerCallBack = new ITicketPagerCallBack() {
                @Override
                public void onNetworkError() {
                    setCurrentStateView(mCoverError);
                }

                @Override
                public void onLoading() {
                    setCurrentStateView(mCoverLoading);
                }

                @Override
                public void onEmpty() {
                    setCurrentStateView(mCoverError);
                }

                @Override
                public void onTicketLoaded(String cover, TicketResult result) {
                    setCurrentStateView(mCover);
                    if (mCover != null && cover != null) {

                        cover = UrlUtils.getCoverPath(cover, 400);


                        Glide.with(TicketActivity.this).load(cover).into(mCover);
                    }


                    if (TextUtils.isEmpty(cover)) {
                        mCover.setImageResource(R.mipmap.ic_launcher);
                    }


                    try {
                        if (result != null && result.getData().getTbk_tpwd_create_response() != null) {
                            mTicketCode.setText(result.getData().getTbk_tpwd_create_response().getData().getModel());
                        }
                    } catch (Exception pE) {
                        pE.printStackTrace();
                    }
                }
            };

            mTicketPresenter.registerViewCallback(mITicketPagerCallBack);
        }
    }

    protected void initVariable() {

    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        release();
    }

    private void release() {
        if (mTicketPresenter != null) {
            mTicketPresenter.unRegisterViewCallback(mITicketPagerCallBack);
        }

        if (mUnbinder != null) {
            mUnbinder.unbind();
        }
    }
}
